#ifndef __UI_H__
#define __UI_H__

#include "event.h"

#define container_of(ptr, type, member) ({                      \
    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
    (type *)( (char *)__mptr - offsetof(type,member) );})

struct list_head {
    struct list_head *next, *prev;
};


#define list_entry(ptr, type, member) \
    container_of(ptr, type, member)

#define list_for_each_entry(pos, head, member)              \
    for (pos = list_entry((head)->next, typeof(*pos), member);  \
         &pos->member != (head);    \
         pos = list_entry(pos->member.next, typeof(*pos), member))

static inline void list_init(struct list_head *head)
{
    head->next = head;
	head->prev = head;
}

static inline int list_empty(const struct list_head *head)
{
    return head->next == head;
}

static inline void __list_add(struct list_head *_new,
                  struct list_head *prev,
                  struct list_head *next)
{
    next->prev = _new;
    _new->next = next;
    _new->prev = prev;
    prev->next = _new;
}

static inline void list_add(struct list_head *_new, struct list_head *head)
{
    __list_add(_new, head, head->next);
}

static inline void list_add_tail(struct list_head *_new, struct list_head *head)
{
    __list_add(_new, head->prev, head);
}

struct ui_struct {
	char name[16];
	uint8_t menu_num;
	uint8_t select;
	uint8_t event_type;
	struct list_head *list;
	struct list_head parent_list;
	struct list_head children_list;
	struct ui_struct *parent;
	void (*show_func)(uint8_t select, struct event_struct *evt);
	void (*app_func)(uint8_t enable);
	void (*action)(struct event_struct *evt);
};

void ui_init(void);

#endif
